<script>
	// #ifdef APP-PLUS
	const request = require('./common/request/index').default
	const utils = require('./common/utils/index.js').default
	import checkappupdate from '@/common/utils/checkappupdate.js'
	import api from '@/common/request/api.js'
	// #endif

	import { mapState, mapGetters, mapMutations, mapActions } from "vuex"
	export default {
		onLaunch: function(options) {
			console.log("App Launch");
			
			// 保存分享参数（H5、小程序）
			options.query.scene && this.setShare({ spm: options.query.scene })
			// 获取配置信息
			this.getConfigInfo()
			this.$nextTick(() => {
				this.updateTabBarText()
			})

			// #ifdef APP-PLUS
			// nvue变量
			this.request = request
			this.utils = utils
			// 锁定竖屏模式
			plus.screen.lockOrientation('portrait-primary');
			// app版本检测
			checkappupdate.check({
				api: this.$BASE_URL + api.common.update.url
			})
			// #endif

			// #ifdef H5
			// 非微信浏览器拦截
			const useragent = navigator.userAgent
			if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
				//window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=888"
			}
			// #endif

			// #ifdef MP-WEIXIN
			// 朋友圈场景
			if(options.scene === 1155) {
				if(options.path != 'pages/home/index') {
					uni.switchTab({
						url: '/pages/home/index'
					})
				}
			}
			// 小程序更新
			const updateManager = uni.getUpdateManager()
			updateManager.onCheckForUpdate(function (res) {
				// 请求完新版本信息的回调
				console.log(res.hasUpdate, "版本检测");
				if(res.hasUpdate) {
					updateManager.onUpdateReady((res) => {
						uni.showModal({
							title: this.$t('message.updateTip'),
							content: this.$t('message.updateContent'),
							success: (res) => {
								if (res.confirm) {
									// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
									updateManager.applyUpdate()
								}
							}
						})
					})
					updateManager.onUpdateFailed(() => {
						// 新版本下载失败
						uni.showModal({
							title: this.$t('message.tip'),
							content: this.$t('message.updateFailed'),
							confirmText: this.$t('message.know')
						});
					})
				}
			})
			// #endif
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		computed: {
			currentLocale() {
				if(this.$i18n && this.$i18n.locale) return this.$i18n.locale
				if(this.$locale && typeof this.$locale.get === 'function') return this.$locale.get()
				return 'default'
			}
		},
		watch: {
			currentLocale() {
				this.updateTabBarText()
			}
		},
		methods: {
			...mapActions("app", ["getConfigInfo"]),
			...mapMutations("app", ["setShare"]),
			updateTabBarText() {
				const tabs = [
					{ index: 0, key: 'tab.home', fallback: '首页' },
					{ index: 1, key: 'tab.watch', fallback: '追剧' },
					{ index: 2, key: 'tab.recommend', fallback: '推荐' },
					{ index: 3, key: 'tab.user', fallback: '我的' },
				]
				if(typeof uni === 'undefined' || typeof uni.setTabBarItem !== 'function') return
				tabs.forEach(({ index, key, fallback }) => {
					const text = (this.$t && this.$t(key)) || fallback
					try {
						uni.setTabBarItem({ index, text })
					} catch (error) {
						console.warn('Failed to set tab bar item', index, error)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uview-ui/index.scss";
	@import "@/common/style/style.scss";
	@import "@/common/style/design-system.scss";
</style>
